from django.contrib import admin
from user.models import UserProfile
from apikey.models import ApiKey, ModelType, AllowModel

# Register your models here.


class ModelTypeAdmin(admin.ModelAdmin):
    search_fields = ['type']
    list_display = ('id', "type")
    list_per_page = 20


class ApiKeyAdmin(admin.ModelAdmin):
    search_fields = ['key']
    list_display = ('key', "model", "state", "creatTime")
    readonly_fields = ('creatTime', )
    list_per_page = 20


class AllowModelAdmin(admin.ModelAdmin):
    list_display = ('type', "defaultCount", "OrdinaryMemberCount", "PremiumMemberCount", "AnnualMemberCount")
    list_per_page = 20

    actions = ['delete_selected']

    def delete_selected(self, request, obj):
        for item in UserProfile.objects.all():
            for delete_item in obj:
                item.count.pop(delete_item.type.type)
            item.save()
        obj.delete()


admin.site.register(ApiKey, ApiKeyAdmin)
admin.site.register(ModelType, ModelTypeAdmin)
admin.site.register(AllowModel, AllowModelAdmin)
